package com.uzhie.jt808.jt1078.entity;

import com.uzhie.jt808.JT808MsgBody;
import com.uzhie.jt808.msg.entity.BitConverter;
import com.uzhie.jt808.msg.entity.MyBuffer;

import java.util.UUID;

/**
 *
 * 查询远程视频目录
 * Created by lilongfei on 2018/7/29.
 */
public class JT_9205 implements JT808MsgBody {

    //消息流水号
    String serialNumber;
    //通道
    byte channel;
    //查询起始时间
    String startTime;
    //查询终止时间
    String endTime;
    //报警标识
    byte[] warns;
    //音视频资源类型 0 音视频 1 音频 2 视频 3 视频或音频
    byte type;
    //主码流,子码流 0 所有 ,1 主码流 2 子码流
    byte stream;
    //远程存储位置 0 全部 1 主存储器 2 灾备存储器
    byte local;

    public JT_9205(int channel,String startDate,String endDate,long warning,int resource,int stream,int eqType){
        this.setSerialNumber(UUID.randomUUID().toString().replace("-",""));
        this.setChannel(channel);
        this.setStartTime(startDate);
        this.setEndTime(endDate);
        this.setWarns(warning);
        this.setType(resource);
        this.setStream(stream);
        this.setLocal(eqType);
    }

    public void setChannel(int channel){
        byte[] channels = BitConverter.GetBytes(channel);
        setChannel(channels[3]);
    }

    public void  setType(int type){
        byte[] types = BitConverter.GetBytes(type);
        setType(types[3]);
    }

    public void setStream(int stream) {
        byte[] streams = BitConverter.GetBytes(stream);
        setStream(streams[3]);
    }

    public void setLocal(int local) {
        byte[] locals = BitConverter.GetBytes(local);
        setLocal(locals[3]);
    }

    public void setWarns(long warn) {
        byte[] warns = BitConverter.getByte64(warn);
        setWarns(warns);
    }

    public byte getChannel() {
        return channel;
    }

    public void setChannel(byte channel) {
        this.channel = channel;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public byte[] getWarns() {
        return warns;
    }

    public void setWarns(byte[] warns) {
        this.warns = warns;
    }

    public byte getType() {
        return type;
    }

    public void setType(byte type) {
        this.type = type;
    }

    public byte getStream() {
        return stream;
    }

    public void setStream(byte stream) {
        this.stream = stream;
    }

    public byte getLocal() {
        return local;
    }

    public void setLocal(byte local) {
        this.local = local;
    }

    public String getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }

    @Override
    public byte[] WriteToBytes() {
        MyBuffer buff = new MyBuffer();
        buff.put(getChannel());
        //设置起始时间
        buff.put(Byte.parseByte(getStartTime().substring(2, 4), 16));
        buff.put(Byte.parseByte(getStartTime().substring(5, 7), 16));
        buff.put(Byte.parseByte(getStartTime().substring(8, 10), 16));
        buff.put(Byte.parseByte(getStartTime().substring(11, 13), 16));
        buff.put(Byte.parseByte(getStartTime().substring(14, 16), 16));
        buff.put(Byte.parseByte(getStartTime().substring(17, 19), 16));
        //设置终止时间
        buff.put(Byte.parseByte(getEndTime().substring(2, 4), 16));
        buff.put(Byte.parseByte(getEndTime().substring(5, 7), 16));
        buff.put(Byte.parseByte(getEndTime().substring(8, 10), 16));
        buff.put(Byte.parseByte(getEndTime().substring(11, 13), 16));
        buff.put(Byte.parseByte(getEndTime().substring(14, 16), 16));
        buff.put(Byte.parseByte(getEndTime().substring(17, 19), 16));
        //设置警告标识
        buff.put(getWarns());
        //音视频资源类型
        buff.put(getType());
        //码流类型
        buff.put(getStream());
        //远程存储位置
        buff.put(getLocal());

        return buff.array();
    }

    @Override
    public void ReadFromBytes(byte[] body) {
        MyBuffer buffer = new MyBuffer();
        setChannel(buffer.get());
        byte[] start = buffer.gets(6);
        setStartTime("20" + String.format("%02X", start[0]) + "-"
                + String.format("%02X", start[1]) + "-"
                + String.format("%02X", start[2]) + " "
                + String.format("%02X", start[3]) + ":"
                + String.format("%02X", start[4]) + ":"
                + String.format("%02X", start[5]));
        byte[] end = buffer.gets(6);
        setEndTime("20" + String.format("%02X", end[0]) + "-"
                + String.format("%02X", end[1]) + "-"
                + String.format("%02X", end[2]) + " "
                + String.format("%02X", end[3]) + ":"
                + String.format("%02X", end[4]) + ":"
                + String.format("%02X", end[5]));
        setWarns(buffer.gets(8));
        setType(buffer.get());
        setStream(buffer.get());
        setLocal(buffer.get());
    }

    public static void main(String[] args){
        JT_9205 jt_9205 = new JT_9205(1,"2018-08-16 00:00:00","2018-08-17 00:00:00",0,3,0,0);
        byte[] b =jt_9205.WriteToBytes();
        System.out.printf(byte2hex(b).trim());
    }
    public static String byte2hex(byte [] buffer){
        String h = "";
        for(int i = 0; i < buffer.length; i++){
            String temp = Integer.toHexString(buffer[i] & 0xFF);
            if(temp.length() == 1){
                temp = "0" + temp;
            }
            h = h +temp;
        }
        return h;

    }
}
